package com.efast.cafe.portal.controller.api;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.service.api.IPortalJdbcService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/frame_center/jdbc")
public class PortalJdbcController {
	@Autowired
	private IPortalJdbcService portalJdbcService;

	@RequestMapping("/tables")
	public @ResponseBody ResultBean queryTables(HttpServletRequest request, HttpServletResponse response) {
		ResultBean result = new ResultBean();
		try {
			result.setSuccess(true);
			result.setObject(portalJdbcService.getTables());
		} catch (ServiceException e) {
			e.printStackTrace();
			result.setSuccess(false);
		}
		return result;

	}

	@RequestMapping("/table/columns")
	public @ResponseBody ResultBean queryColumns(HttpServletRequest request, HttpServletResponse response,
			@RequestBody Map<String, String> map) {
		ResultBean result = new ResultBean();
		try {
			result.setSuccess(true);
			if (map.containsKey("tableName")) {
				String tableName = map.get("tableName");
				result.setObject(portalJdbcService.getColumns(tableName));
			}
		} catch (ServiceException e) {
			result.setSuccess(false);
			e.printStackTrace();
		}
		return result;

	}
}
